package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.login.LoginUserHolder;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
@Autowired
private RoomInfoMapper roomInfoMapper;
@Autowired
private GraphInfoMapper graphInfoMapper;
@Autowired
private AttrValueMapper attrValueMapper;
@Autowired
private FacilityInfoMapper facilityInfoMapper;
@Autowired
private LabelInfoMapper labelInfoMapper;
@Autowired
private PaymentTypeMapper paymentTypeMapper;
@Autowired
private FeeValueMapper feeValueMapper;
@Autowired
private LeaseTermMapper leaseTermMapper;
@Autowired
private ApartmentInfoService apartmentInfoService;
@Autowired
private BrowsingHistoryService browsingHistoryService;
@Autowired
private RedisTemplate<String,Object> redisTemplate;
    @Override
    public IPage<RoomItemVo> findByPageItems(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
         return roomInfoMapper.findByPageItems(page,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        String key = RedisConstant.APP_ROOM_PREFIX+id;
        RoomDetailVo roomDetailVo = (RoomDetailVo) redisTemplate.opsForValue().get(key);
        if(roomDetailVo==null){
            //查询公寓信息
            RoomInfo roomInfo = roomInfoMapper.selectById(id);
            if (roomInfo == null)
                return null;
            //查询图片
            List<GraphVo> graphVoList = graphInfoMapper.selectDetail(ItemType.ROOM, id);
            //查询属性
            List<AttrValueVo> attrValueVoList = attrValueMapper.selectDetail(id);
            //查询配套
            List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectDetail(id);
            //查询标签
            List<LabelInfo> labelInfoList = labelInfoMapper.selectDetail(id);
            //查询支付方式
            List<PaymentType> paymentTypes = paymentTypeMapper.selectListByRoomId(id);
            //查询杂费
            List<FeeValueVo> feeValueVoList = feeValueMapper.selectDetail(id);
            //查询租期
            List<LeaseTerm> leaseTermList = leaseTermMapper.selectDetail(id);
            //查询公寓信息
            ApartmentItemVo apartmentItemVos = apartmentInfoService.selectDetail(roomInfo.getApartmentId());
            roomDetailVo = new RoomDetailVo();
            BeanUtils.copyProperties(roomInfo,roomDetailVo);

            roomDetailVo.setApartmentItemVo(apartmentItemVos);
            roomDetailVo.setFacilityInfoList(facilityInfoList);
            roomDetailVo.setLabelInfoList(labelInfoList);
            roomDetailVo.setGraphVoList(graphVoList);
            roomDetailVo.setFeeValueVoList(feeValueVoList);
            roomDetailVo.setAttrValueVoList(attrValueVoList);
            roomDetailVo.setLeaseTermList(leaseTermList);
            roomDetailVo.setPaymentTypeList(paymentTypes);
            redisTemplate.opsForValue().set(key,roomDetailVo);
        }

        //保存浏览历史
        browsingHistoryService.saveHistory(LoginUserHolder.getLoginUser().getUserID(),id);
        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> findRoomByidandPages(IPage<RoomItemVo> page, Long id) {
        return roomInfoMapper.findRoomByidandPages(page,id);
    }
}




